package cn.easy.jacob.service;

import cn.easy.jacob.utils.BaseUtils;
import cn.easy.jacob.utils.Constants;
import cn.easy.jacob.utils.JacobUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class JacobService {

    public File fileDispatch(MessageExt messageExt) throws Exception {
        String fileUploadDir = Constants.FILE_UPLOAD_DIR;

        File file = null;
        //文件转换
        if (messageExt.getTags().equals(Constants.REPORT_TEMPLSTE_CONVERT)) {
            String srcPath = BaseUtils.getFile(fileUploadDir, ".doc", messageExt);
            String descPath = fileUploadDir + File.separator + System.currentTimeMillis() + ".docx";
            file = convertDocFmt(srcPath, descPath, Constants.DOCX_FMT);
        }
        if (messageExt.getTags().equals(Constants.UPDATE_TABLES)) {
            String srcPath = BaseUtils.getFile(fileUploadDir, ".docx", messageExt);
            file = updateTable(srcPath);
        }
        return file;
    }

    /**
     * @param srcPath  the doc path 源文件
     * @param descPath the docx path 目标文件
     * @param fmt      the fmt 所转格式
     */
    public File convertDocFmt(String srcPath, String descPath, int fmt) throws Exception {
        // 实例化ComThread线程与ActiveXComponent
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        try {
            Dispatch doc = JacobUtil.openDocument(app, srcPath, true);
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{descPath, new Variant(fmt)}, new int[1]);
            //关闭word文件
            Dispatch.call(doc, "Close", new Variant(false));

            //删除源文件
            File file = new File(srcPath);
            file.delete();
            return new File(descPath);
        } catch (Exception e) {
            throw e;
        } finally {
            // 释放线程与ActiveXComponent
            app.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }

    /**
     * @param srcPath the docx path 源文件
     */
    public File updateTable(String srcPath) throws Exception {
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        try {
            Dispatch doc = JacobUtil.openDocument(app, srcPath, false);

            //获取所有表格
            Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
            int count = Dispatch.get(tables, "Count").getInt();
            for (int i = 0; i < count; i++) {
                Dispatch table = Dispatch.call(tables, "Item", new Variant(i + 1)).toDispatch();
                Dispatch.call(table, "Select");
                JacobUtil.setCellVerticalAlign(1);
                JacobUtil.setParaFormat(0, 0, 0.01);
                JacobUtil.setTableBorderWidth(table, 1.5);

                //自适应
                Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
                Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
                JacobUtil.setTableFormat(rows, 1, 1, 0.7);
                Dispatch.call(cols, "AutoFit");


                //首行加粗及全局字体设置
                Dispatch rowFirst = Dispatch.call(rows, "First").toDispatch();
                Dispatch.call(rows, "Select");
                JacobUtil.setFont(false, false, false, "", "10", "宋体");
                Dispatch.call(rowFirst, "Select");
                JacobUtil.setFont(true, false, false, "", "10", "宋体");

                //合计行加粗
                int rowCount = Dispatch.get(rows,"Count").getInt();
                for (int r = 1; r <= rowCount; r++) {
                    Dispatch row = Dispatch.call(rows, "Item", new Variant(r)).toDispatch();
                    Dispatch.call(row, "Select");
                    if (JacobUtil.find("合计")){
                        Dispatch.call(row, "Select");
                        JacobUtil.setFont(true, false, false, "", "10", "宋体");
                    }
                }
            }

            //更新目录
            Dispatch activeDocument = app.getProperty("ActiveDocument").toDispatch();
            JacobUtil.updateDirectory(activeDocument);
            // 关闭并保存
            Dispatch.call(doc, "Close", new Variant(true));
            return new File(srcPath);
        } catch (Exception e) {
            throw e;
        } finally {
            app.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }
}
